//
//  BranchOpenPartyServiceCenterViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/6/14.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "BranchOpenPartyServiceCenterViewController.h"
#import "PartyServiceCenterTableViewCell.h"
#import "PartyServiceCenterModel.h"
#import "PartyServiceCenterHeaderView.h"
#import "PartyServiceCenterHeaderModel.h"
#import "LPArrayNoDataTableViewCell.h"
#import "PartyServiceContactViewController.h"
#define CELL @"cell"
#define NODATA @"NODATA"
#import "ShareOpenListModel.h"
#import "ShareOrderActServiceViewController.h"
@interface BranchOpenPartyServiceCenterViewController ()<UITableViewDelegate,UITableViewDataSource,PartyServiceCenterTableCellDelegate>
/**    */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;
/**  与我联系  */
@property (strong, nonatomic) UIButton  *contactButton;

/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  党建服务中心的所有数据  */
@property (strong, nonatomic) PartyServiceCenterHeaderModel  *allInforModel;
/**  头部  */
@property (strong, nonatomic) PartyServiceCenterHeaderView  *headerView;
/**  判断是否是第一次  */
@property (assign, nonatomic) BOOL   isFirst;

@end

@implementation BranchOpenPartyServiceCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.isFirst = YES;
    self.titlelabel.text = self.model.DEPT_NAME;
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page++;
        [weakSelf netWorking_footer];
    }];
    
    [self.tableView.mj_header beginRefreshing];
    [self layoutUI];
}

#pragma mark - 数据

//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"ID": self.model.ID,
                              @"PAGE_NUMBER": page,
                              @"SHOW_NUMBER": @"5"};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getpudongorgactionLoginInfogetConcenterAddressById] Parameter:paramer Success:^(id object) {
       
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            if (weakSelf.isFirst) {
                weakSelf.allInforModel = [PartyServiceCenterHeaderModel yy_modelWithJSON:object[@"ORG_DEPT"]];
                [weakSelf addHeader];
            }
            
           
            NSArray *array = weakSelf.allInforModel.RESOURCE;
            
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[PartyServiceCenterModel class] json:array] mutableCopy] ;
            [weakSelf.tableView reloadData];
            
            
        }
        
        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"ID": self.model.ID,
                              @"PAGE_NUMBER": page,
                              @"SHOW_NUMBER": @"5"};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getpudongorgactionLoginInfogetConcenterAddressById] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = object[@"ORG_DEPT"][@"RESOURCE"];
            [weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[PartyServiceCenterModel class] json:array]] ;
            [weakSelf.tableView reloadData];
            
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
    
}

- (void)clickButton{
//    PartyServiceContactViewController *vc = [PartyServiceContactViewController new];
//    vc.model = self.model;
//    [self.navigationController pushViewController:vc animated:YES];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@" , self.allInforModel.DEPTCONTACTS_MOBILE]]];

}

#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];

    
    [self.view addSubview:self.contactButton];
    self.contactButton.sd_layout
    .bottomSpaceToView(self.view, 7.5)
    .centerXEqualToView(self.view)
    .heightIs(35)
    .widthIs(120);
}


- (void)addHeader{
        self.isFirst = NO;
    CGFloat headerHeight = 0.0;
    CGFloat height = [CalculateLabelHeight CalculateLabelHeightWithString:self.allInforModel.DEPT_DEADDRESS font:16 * [UIScreen zj_textFontScale] width:[UIScreen zj_screenWith] - 80 * [UIScreen zj_screenWScale]].height;
    if (height > 20 * [UIScreen zj_screenHScale]) {
        headerHeight = 240 * [UIScreen zj_screenHScale] + height;
    }else{
        headerHeight =  260 * [UIScreen zj_screenHScale];
    }
    
    
    PartyServiceCenterHeaderView *header = [[PartyServiceCenterHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, headerHeight)];
    header.model = self.allInforModel;
    self.tableView.tableHeaderView = header;
    self.headerView = header;

}

#pragma mark - PartyServiceCenterTableCellDelegate
- (void)PartyServiceCenterTableWithCell_index:(NSInteger)index section:(NSInteger)section{
    ShareOpenListModel *resouce = [ShareOpenListModel new];
    PartyServiceCenterModel *model = self.dataSouce[index];
    resouce.ID = model.ID;
    resouce.TORANG = model.TORANG;
    resouce.RESOURCETYPE = model.RESOURCETYPE;
    resouce.PEOPLE_NUM = model.PEOPLE_NUM;
    ShareOrderActServiceViewController *vc = [ShareOrderActServiceViewController new];
    vc.model = resouce;
    [self.navigationController pushViewController:vc animated:YES];

}


#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.dataSouce.count == 0) {
        return 1;
    }else{
        return self.dataSouce.count;
    }
    
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataSouce.count == 0) {
        return 120 * [UIScreen zj_screenHScale];
    }else{
        PartyServiceCenterModel *model = self.dataSouce[indexPath.row];
        return 305 * [UIScreen zj_screenHScale] + model.HEIGHT;
    }
   
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dataSouce.count == 0) {
        LPArrayNoDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NODATA forIndexPath:indexPath];
        return cell;

    }else{
        PartyServiceCenterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL forIndexPath:indexPath];
        cell.model = self.dataSouce[indexPath.row];
        cell.row = indexPath.row;
        cell.section = indexPath.section;
        cell.delegate = self;
        return cell;

    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    TenGrayShadowView *ten = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 40 * [UIScreen zj_screenHScale])];
    ten.backgroundColor = [UIColor themeGrayColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(18, 0, 100, ten.height)];
    label.text = @"服务项目:";
    label.textColor = [UIColor textBlackColor];
    label.font = LYPFONT(17);
    [ten addSubview:label];
    return ten;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40 * [UIScreen zj_screenHScale];
}

/**  取消header悬浮  */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.tableView)
        
    {
        
        UITableView *tableview = (UITableView *)scrollView;
        
        CGFloat sectionHeaderHeight = 40 * [UIScreen zj_screenHScale];
        
        CGFloat sectionFooterHeight = 0;
        
        CGFloat offsetY = tableview.contentOffset.y;
        
        if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
            
        {
            
            tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);
            
        }else if (offsetY >= sectionHeaderHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight)
            
        {
            
            tableview.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);
            
        }else if (offsetY >= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height)         {
            
            tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight), 0);
            
        }
        
    }
    
}


#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight - 50) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        [_tableView registerClass:[PartyServiceCenterTableViewCell class] forCellReuseIdentifier:CELL];
         [_tableView registerClass:[LPArrayNoDataTableViewCell class] forCellReuseIdentifier:NODATA];
        
    }
    return _tableView;
}
- (UIButton *)contactButton{
    if (!_contactButton) {
        _contactButton = ({
            UIButton *button = [UIButton new];
            [button setTitle:@"与我联系" forState:UIControlStateNormal];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.backgroundColor = [UIColor themeColor];
            [button addTarget:self action:@selector(clickButton) forControlEvents:UIControlEventTouchUpInside];
            button.layer.cornerRadius = 8;
            button.layer.masksToBounds = YES;
            button;
            
        });
    }
    return _contactButton;
}


- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

- (PartyServiceCenterHeaderModel *)allInforModel{
    if (!_allInforModel) {
        _allInforModel = [PartyServiceCenterHeaderModel new];
    }
    return _allInforModel;
}
@end
